<?php
defined('_JEXEC') or die();

class TableReservations extends JTable
{
	var $id = null;
	var $start_time = null;
	var $end_time = null;
	var $title = null;

	function __construct(&$db)
	{
		parent::__construct( '#__booking_reservations', 'id', $db );
	}
	
	function getReservations($resource_id, $start_time, $end_time){
		$q = " SELECT 
				id, 
				DATE_FORMAT(start_time, '%Y-%m-%dT%H:%i:%s+08:00')  start,
				DATE_FORMAT(end_time, '%Y-%m-%dT%H:%i:%s+08:00') end, 
				title 
				FROM #__booking_reservations 
				where resource_id =".$resource_id." 
				and start_time >='".$start_time."' 
				and start_time < '".$end_time."'";
		$this->_db->setQuery($q);
		$rows = $this->_db->loadObjectList();
		return $rows;
	}
	
	function isBooking($id, $resource_id, $start_time, $end_time){
		$q = " SELECT id
				FROM #__booking_reservations 
				where
					id <>".$id."
				and resource_id =".$resource_id." 
				and (
					(start_time >'".$start_time."' and start_time <= '".$end_time."')
					or (end_time >'".$start_time."' and end_time <= '".$end_time."')
				)";		
		$this->_db->setQuery($q);
		$rows = $this->_db->loadObject();
		if($rows == null) return false;
		return true;
	}
}
?>